//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "UnitOfMeasureConversion.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Experimental/Commodities/Quantity.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phUnitOfMeasureConversion = NULL;
#endif
        _ppUnitOfMeasureConversion = new boost::shared_ptr<QuantLib::UnitOfMeasureConversion> (new QuantLib::UnitOfMeasureConversion ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (Cephei::QL::Experimental::Commodities::ICommodityType^ commodityType, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ source, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ target, Double conversionFactor) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCommodityType^ _CcommodityType;
    CUnitOfMeasure^ _Csource;
    CUnitOfMeasure^ _Ctarget;
    try
    {
#ifdef HANDLE
        _phUnitOfMeasureConversion = NULL;
#endif
        _CcommodityType = safe_cast<CCommodityType^> (commodityType);
        _CcommodityType->Lock();
        QuantLib::CommodityType& _commodityType = static_cast<QuantLib::CommodityType&> (_CcommodityType->GetReference ()); 
        _Csource = safe_cast<CUnitOfMeasure^> (source);
        _Csource->Lock();
        QuantLib::UnitOfMeasure& _source = static_cast<QuantLib::UnitOfMeasure&> (_Csource->GetReference ()); 
        _Ctarget = safe_cast<CUnitOfMeasure^> (target);
        _Ctarget->Lock();
        QuantLib::UnitOfMeasure& _target = static_cast<QuantLib::UnitOfMeasure&> (_Ctarget->GetReference ()); 
        QuantLib::Real _conversionFactor = (QuantLib::Real)ValueHelper::Convert (conversionFactor);
        _ppUnitOfMeasureConversion = new boost::shared_ptr<QuantLib::UnitOfMeasureConversion> (new QuantLib::UnitOfMeasureConversion ( _commodityType,  _source,  _target,  _conversionFactor ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcommodityType != nullptr) _CcommodityType->Unlock();
        if (_Csource != nullptr) _Csource->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (boost::shared_ptr<QuantLib::UnitOfMeasureConversion>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversion = NULL;
#endif
	_ppUnitOfMeasureConversion = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (QuantLib::UnitOfMeasureConversion& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phUnitOfMeasureConversion = NULL;
#endif
	_ppUnitOfMeasureConversion = new boost::shared_ptr<QuantLib::UnitOfMeasureConversion> (&childNative);
    
    _UnitOfMeasureConversionOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (CUnitOfMeasureConversion^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversion = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUnitOfMeasureConversion = new boost::shared_ptr<QuantLib::UnitOfMeasureConversion> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversion = NULL;
#endif
	if (!t->IsSubclassOf(CUnitOfMeasureConversion::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (QuantLib::Handle<QuantLib::UnitOfMeasureConversion>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasureConversion = &childNative;
	_ppUnitOfMeasureConversion = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasureConversion>>(childNative.currentLink());
    
    _UnitOfMeasureConversionOwner = owner;
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (QuantLib::Handle<QuantLib::UnitOfMeasureConversion> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phUnitOfMeasureConversion = &childNative;
	_ppUnitOfMeasureConversion = &static_cast<boost::shared_ptr<QuantLib::UnitOfMeasureConversion>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CUnitOfMeasureConversion (QuantLib::UnitOfMeasureConversion childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phUnitOfMeasureConversion = NULL;
#endif
	_ppUnitOfMeasureConversion = new boost::shared_ptr<QuantLib::UnitOfMeasureConversion> (new QuantLib::UnitOfMeasureConversion (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::~CUnitOfMeasureConversion ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppUnitOfMeasureConversion != NULL)
    {
	    delete _ppUnitOfMeasureConversion;
        _ppUnitOfMeasureConversion = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::!CUnitOfMeasureConversion ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppUnitOfMeasureConversion != NULL)
    {
	    delete _ppUnitOfMeasureConversion;
    }
}
QuantLib::UnitOfMeasureConversion& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::GetReference ()
{
    if (_ppUnitOfMeasureConversion == NULL) throw gcnew NativeNullException ();
	return **_ppUnitOfMeasureConversion;
}
boost::shared_ptr<QuantLib::UnitOfMeasureConversion>& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::GetShared ()
{
    if (_ppUnitOfMeasureConversion == NULL) throw gcnew NativeNullException ();
	return *_ppUnitOfMeasureConversion;
}
QuantLib::UnitOfMeasureConversion* Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::GetPointer ()
{
    if (_ppUnitOfMeasureConversion == NULL) throw gcnew NativeNullException ();
	return &**_ppUnitOfMeasureConversion;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UnitOfMeasureConversion>& Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::GetHandle ()
{
	if (_phUnitOfMeasureConversion == NULL)
	{
		_phUnitOfMeasureConversion = new Handle<QuantLib::UnitOfMeasureConversion> (*_ppUnitOfMeasureConversion);
	}
	return *_phUnitOfMeasureConversion;
}
#endif
bool Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::HasNative () 
{
	return (_ppUnitOfMeasureConversion != NULL);
}

Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Chain (Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ r1, Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ r2)
{
    CUnitOfMeasureConversion^ _Cr1;
    CUnitOfMeasureConversion^ _Cr2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cr1 = safe_cast<CUnitOfMeasureConversion^> (r1);
        _Cr1->Lock();
        QuantLib::UnitOfMeasureConversion& _r1 = static_cast<QuantLib::UnitOfMeasureConversion&> (_Cr1->GetReference ()); 
        _Cr2 = safe_cast<CUnitOfMeasureConversion^> (r2);
        _Cr2->Lock();
        QuantLib::UnitOfMeasureConversion& _r2 = static_cast<QuantLib::UnitOfMeasureConversion&> (_Cr2->GetReference ()); 
    	QuantLib::UnitOfMeasureConversion _rv = (QuantLib::UnitOfMeasureConversion)(*_ppUnitOfMeasureConversion)->chain ( _r1,  _r2 );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cr1 != nullptr) _Cr1->Unlock();
        if (_Cr2 != nullptr) _Cr2->Unlock();
    }
}
String^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Code::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppUnitOfMeasureConversion)->code ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::CommodityType::get ()
{
    try
    {
    	QuantLib::CommodityType& _rv = (QuantLib::CommodityType&)(*_ppUnitOfMeasureConversion)->commodityType ( );   
        Cephei::QL::Experimental::Commodities::CCommodityType^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CCommodityType (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::ConversionFactor::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppUnitOfMeasureConversion)->conversionFactor ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Convert (Cephei::QL::Experimental::Commodities::IQuantity^ quantity)
{
    CQuantity^ _Cquantity;
    try
    {
        _Cquantity = safe_cast<CQuantity^> (quantity);
        _Cquantity->Lock();
        QuantLib::Quantity& _quantity = static_cast<QuantLib::Quantity&> (_Cquantity->GetReference ()); 
    	QuantLib::Quantity _rv = (QuantLib::Quantity)(*_ppUnitOfMeasureConversion)->convert ( _quantity );   
        Cephei::QL::Experimental::Commodities::CQuantity^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CQuantity (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cquantity != nullptr) _Cquantity->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Source::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppUnitOfMeasureConversion)->source ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Target::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppUnitOfMeasureConversion)->target ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Experimental::Commodities::UnitOfMeasureConversion::TypeEnum Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion::Type::get ()
{
    try
    {
    	QuantLib::UnitOfMeasureConversion::Type _rv = (QuantLib::UnitOfMeasureConversion::Type)(*_ppUnitOfMeasureConversion)->type ( );   
        QL::Experimental::Commodities::UnitOfMeasureConversion::TypeEnum _nrv = (QL::Experimental::Commodities::UnitOfMeasureConversion::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion_Factory::Create ()
{
    return gcnew CUnitOfMeasureConversion ();
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasureConversion^ Cephei::QL::Experimental::Commodities::CUnitOfMeasureConversion_Factory::Create (Cephei::QL::Experimental::Commodities::ICommodityType^ commodityType, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ source, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ target, Double conversionFactor)
{
    return gcnew CUnitOfMeasureConversion ( commodityType,  source,  target,  conversionFactor);
}
